package com.qing.springboot01.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qing.springboot01.common.BaseContext;
import com.qing.springboot01.common.R;
import com.qing.springboot01.entity.ShoppingCart;
import com.qing.springboot01.service.ShoppingCartService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author tina
 * @since 2023-06-26 04:09:20
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    /**
     * 加入购物车
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart,HttpSession session) {
        log.info(shoppingCart.toString());

        // 获取用户id
        long user = (long)session.getAttribute("user");
        shoppingCart.setUserId(user);

        // 查询当前菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,user);
        if (dishId!=null){
            //菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if (cartServiceOne!=null){
            int number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne); //更新购物车
        }else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }


    /**
     * 购物车减少
     * */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        log.info("减少数量 {}",shoppingCart);
        // 设置用户ID
        Long userID = (Long)session.getAttribute("user");
        shoppingCart.setUserId(userID);
        // 根据用户ID和菜品ID或者套餐ID确定数据
        final Long dishId = shoppingCart.getDishId();
        final LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userID);
        if(dishId!=null){
            // 购物车减少的是菜品
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            // 购物车减少的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        final ShoppingCart one = shoppingCartService.getOne(wrapper);
        if(one.getNumber()-1==0){
            // 如果已经是最后一份.减少直接删除
            shoppingCartService.remove(wrapper);
            shoppingCart.setNumber(one.getNumber()-1);
            return R.success(one);
        }else{
            shoppingCart.setNumber(one.getNumber()-1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.update();
        }

        return R.success(one);
    }

    /**
     *查看购物车
     * */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
        log.info("查看购物车" );
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,session.getAttribute("user"));
        wrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 清空购物车
     * */

    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,session.getAttribute("user"));
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }

    
}
